﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StockCrawlerLib.DAL;

namespace StockCrawler.UserControl
{
    public partial class ucStockFilter : System.Web.UI.UserControl
    {
        private StockRepository _stockRepo = new StockRepository();
        private HistoryRepository _hisRepo = new HistoryRepository();

        public event EventHandler StockFilterEvent;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cboSelect.SelectedIndex = 5;
                lblNumDaysBefore.Text = "Số ngày giảm liên tục";
                cboNumDaysBefore.SelectedIndex = 2;
                lblNumDaysAfter.Text = "Sau khi tăng liên tục";
                cboNumDaysAfter.SelectedIndex = 0;
                cboChangedRange.SelectedIndex = 0;
                cboVolumnRange.SelectedIndex = 0;
                InitMasterGrid2();
            }
        }

        private void InitMasterGrid2()
        {
            int loaiCP = int.Parse(cboLoaiCP.SelectedItem.Value.ToString());
            IQueryable result = null;
            if (cboSelect.SelectedIndex == 0) //All
            {
                chkIncludeNoneChangeBefore.Enabled = false;
                chkIncludeNoneChangeAfter.Enabled = false;
            }
            else
            {
                chkIncludeNoneChangeBefore.Enabled = true;
                chkIncludeNoneChangeAfter.Enabled = true;
            }

            if (cboSelect.SelectedIndex == 0) //All
            {
                result = _stockRepo.GetDefaultStockList(loaiCP);
            }
            else if (cboSelect.SelectedIndex == 1) //Remark
            {
                result = _stockRepo.GetRemarkStockList(loaiCP);
            }
            else if (cboSelect.SelectedIndex == 6) //New Stocks
            {
                lblNumDaysBefore.Text = "Số ngày lên sàn ";
                int numDaysBefore = int.Parse(cboNumDaysBefore.SelectedItem.Value.ToString());
                result = _stockRepo.GetNewStockList(loaiCP, numDaysBefore);
            }
            else if (cboSelect.SelectedIndex == 7) //Khối lượng tăng đột biến
            {
                lblNumDaysBefore.Text = "KL tăng đột biến";
                int numDaysBefore = int.Parse(cboNumDaysBefore.SelectedItem.Value.ToString());
                int volumeRange = int.Parse(cboVolumnRange.SelectedItem.Value.ToString());
                int volumeTimes = int.Parse(cboTimes.SelectedItem.Value.ToString());
                result = _hisRepo.SelectKLDotBien(loaiCP, numDaysBefore, volumeRange, volumeTimes, true);
            }
            else if (cboSelect.SelectedIndex == 8) //Khối lượng giảm đột biến
            {
                lblNumDaysBefore.Text = "KL giảm đột biến";
                int numDaysBefore = int.Parse(cboNumDaysBefore.SelectedItem.Value.ToString());
                int volumeRange = int.Parse(cboVolumnRange.SelectedItem.Value.ToString());
                int volumeTimes = int.Parse(cboTimes.SelectedItem.Value.ToString());
                result = _hisRepo.SelectKLDotBien(loaiCP, numDaysBefore, volumeRange, volumeTimes, false);
            }
            else
            {
                string changedRange = cboChangedRange.SelectedItem.Value.ToString();
                int volumeRange = int.Parse(cboVolumnRange.SelectedItem.Value.ToString());
                int numDaysBefore = int.Parse(cboNumDaysBefore.SelectedItem.Value.ToString());
                int numDaysAfter = int.Parse(cboNumDaysAfter.SelectedItem.Value.ToString());

                bool isIncludeNoneChangeBefore = false;
                if (chkIncludeNoneChangeBefore.Checked)
                    isIncludeNoneChangeBefore = true;

                bool isIncludeNoneChangeAfter = false;
                if (chkIncludeNoneChangeAfter.Checked)
                    isIncludeNoneChangeAfter = true;

                if (cboSelect.SelectedIndex == 2) //CE
                {
                    lblNumDaysBefore.Text = "Số ngày tăng trần liên tục";
                    lblNumDaysAfter.Text = "Sau khi giảm sàn liên tục";
                    cboNumDaysBefore.SelectedIndex = 0;
                    result = _hisRepo.SelectCEFLFilter(loaiCP, true, numDaysAfter, numDaysBefore, changedRange, volumeRange);
                }
                else if (cboSelect.SelectedIndex == 3) //FL
                {
                    lblNumDaysBefore.Text = "Số ngày giảm sàn liên tục";
                    lblNumDaysAfter.Text = "Sau khi tăng trần liên tục";
                    cboNumDaysBefore.SelectedIndex = 0;
                    result = _hisRepo.SelectCEFLFilter(loaiCP, false, numDaysAfter, numDaysBefore, changedRange, volumeRange);
                }
                else if (cboSelect.SelectedIndex == 4) //Increase
                {
                    lblNumDaysBefore.Text = "Số ngày tăng liên tục";
                    lblNumDaysAfter.Text = "Sau khi giảm liên tục";
                    result = _hisRepo.SelectByFilter2(loaiCP, true, numDaysAfter, isIncludeNoneChangeAfter, numDaysBefore, isIncludeNoneChangeBefore, changedRange, volumeRange);
                }
                else if (cboSelect.SelectedIndex == 5) //Decrease
                {
                    lblNumDaysBefore.Text = "Số ngày giảm liên tục";
                    lblNumDaysAfter.Text = "Sau khi tăng liên tục";
                    result = _hisRepo.SelectByFilter2(loaiCP, false, numDaysAfter, isIncludeNoneChangeAfter, numDaysBefore, isIncludeNoneChangeBefore, changedRange, volumeRange);
                }
            }
            Session["StockFilterResult"] = result;

            if (this.StockFilterEvent != null)
                this.StockFilterEvent(new object(), new EventArgs());
        }
        protected void cboNumDaysBefore_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitMasterGrid2();
        }

        protected void cboVolumnRange_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitMasterGrid2();
        }

        protected void cboChangedRange_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitMasterGrid2();
        }

        protected void cboNumDaysAfter_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitMasterGrid2();
        }

        protected void chkIncludeNoneChangeBefore_CheckedChanged(object sender, EventArgs e)
        {
            InitMasterGrid2();
        }

        protected void chkIncludeNoneChangeAfter_CheckedChanged(object sender, EventArgs e)
        {
            InitMasterGrid2();
        }

        protected void cboSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            InitMasterGrid2();
        }

        protected void cboLoaiCP_SelectedIndexChanged(object sender, EventArgs e)
        {

            InitMasterGrid2();
        }
    }
}